<?php
/**
 * 功能说明 随机字符串
 * @date: 2016-10-17
 * @author: tzboy
 * @param $length
 * @return string
 */
function getRandChar($length){
   $str = null;
   $strPol = "ABCDEFGHJKMNPQRSTUVWXYZ23456789";
   $max = strlen($strPol)-1;

   for($i=0;$i<$length;$i++){
    $str.=$strPol[rand(0,$max)];//rand($min,$max)生成介于min和max两个数之间的一个随机整数
   }

   return $str;
}
/**
 * @date: 2016-11-17
 * @author: tzboy
 * @return array
 */
function searchTime($search_time,$timeNum=6){
    $timeSlot=array();
    if($search_time['start_date']&&$search_time['end_date']){
        $timeSlot = array(
            'begin'=>$search_time['start_date'],
            'end'=>$search_time['end_date'],
        );
    }else{
        $timeSlot = array(
            'begin'=>date("Y-m-d", strtotime(' -'.$timeNum.'day')),
            'end'=>date("Y-m-d", strtotime('now')),
        );
    }
    $timeiffData = timediff(strtotime($timeSlot['begin'].' 00:00:00'),strtotime($timeSlot['end'].'23:59:59'));
    $timeDayNum = $timeiffData['day'];
    $timeSlot['timeDayNum'] = $timeDayNum;
    return $timeSlot;
}
/**
 * 功能说明 时间段差值
 * @date: 2016-11-17
 * @author: tzboy
 * @param $begin_time ,$end_time,
 * @return array
 */
function timediff( $begin_time, $end_time ){
    if ( $begin_time < $end_time ) {
        $starttime = $begin_time;
        $endtime = $end_time;
    } else {
        $starttime = $end_time;
        $endtime = $begin_time;
    }
    $timediff = $endtime - $starttime;
    $days = intval( $timediff / 86400 );
    $remain = $timediff % 86400;
    $hours = intval( $remain / 3600 );
    $remain = $remain % 3600;
    $mins = intval( $remain / 60 );
    $secs = $remain % 60;
    $res = array( "day" => $days, "hour" => $hours, "min" => $mins, "sec" => $secs );
    return $res;
}
/**
 * 功能说明 生成图形验证码
 * @date: 2015-12-22
 * @author: chenxianwen
 * @param $fontSize ,$length,$useNoise,$useCurve
 * @return string
 */
function verify($fontSize = 30, $length = 4, $useNoise = false, $useCurve = false)
{
    $config = array(
        "fontSize" => $fontSize,//验证码字体大小
        "length" => $length,//验证码长度
        'useNoise' => $useNoise,//是否关闭杂点
        'useCurve' => $useCurve,//是否关闭波浪线
    );
    $verify = new \Think\Verify($config);
    return $verify->entry();
}

function check_verify($code, $id = '')
{
    $verify = new \Think\Verify();
    return $verify->check($code, $id);
}

/**
 * uploads_file
 * 函数用途描述: 上传文件配置
 * @date 2016-03-31
 * @author   chenxianwen
 * @param int $maxSize 上传文件大小
 * @param array $saveName 上传文件的名称
 * @param array $subName
 * @param string $rootPath
 * @return  array|bool|string
 */
function uploads_file( $rootPath='./Uploads/',$maxSize = 0, $saveName = array(),$subName = array(),$savePath='')
{
    if(empty($savePath)){
        $savePath = date("Y-m-d").'/'.time().'/';
    }
    if (!file_exists($rootPath)) {
        mkdir($rootPath, 0777, true);
    }
    $config = array(
        'maxSize' => $maxSize, //上传的文件大小限制 (0-不做限制)
        'subName' => $subName, //子目录创建方式，[0]-函数名，[1]-参数，多个参数使用数组
        'rootPath' => $rootPath, //保存根路径
        'savePath' => $savePath,
        'saveName' => $saveName, //上传文件命名规则，[0]-函数名，[1]-参数，多个参数使用数组
        'replace' => true, //存在同名是否覆盖
    );
    $Upload = new Think\Upload($config);
    $data = $Upload->upload();
    $result = array(
        'statusCode'=>300,
        'message'=>'上传失败！',
        'filename'=>''
    );
    if (!$data) {
        $data = $Upload->getError();
    }else{
        $result = array(
            'statusCode'=>200,
            'message'=>'上传成功！',
            'filepath'=>C('upload_save_url').$data['file']['savepath'].$data['file']['savename'],
            'filename'=>$data['file']['savename']
        );
    }
    return $result;
}

/**
 * [getPic description]
 * 获取文本中首张图片地址
 * @author   tzboy
 * @param  [type] $content [description]
 * @return [type]          [description]
 */
function getPic($content,$def_img){
    if(!$def_img){
        $str=C('def_img');
    }else{
        $str=$def_img;
    }
    if(preg_match_all("/(src)=([\"|']?)([^ \"'>]+\.(gif|jpg|jpeg|bmp|png))\\2/i", $content, $matches)) {
        $str=$matches[3][0];
    }
    if(!strchr($str,'http')){
        $str = C('website_url').$str;
    }
    return $str;
}

function image_name($name){
    return md5(date("ymd").$name);
}

function checkPhone($phone){
    $data = D("User")->where(array("phone"=>$phone))->find();
    return $data?true:false;
}

function getConfigContent($oneKey,$twoKey){
    $oneConfig = C($oneKey);
    return $oneConfig[$twoKey];
}
/**
 * page
 * 函数用途描述:
 * @date 2015-12-22
 * @author   chenxianwen
 * @param $Page
 * @return  void
 */
function page($Page)
{
    $Page->rollPage = 5;//最多显示5个分页页码
    $Page->lastSuffix = false;
    $Page->setConfig("prev", "上一页");
    $Page->setConfig("next", "下一页");
    $Page->setConfig("first", "首页");
    $Page->setConfig("last", "尾页");
}

function send_post($data, $url)
{
    $len = strlen($data);
    $url = str_replace(' ', '+', $url);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "$url");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json', "Content-length:$len"));
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_TIMEOUT, 1000);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
    curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);//严格校验
    $output = curl_exec($ch);
    $errorCode = curl_errno($ch);
    curl_close($ch);
    if (0 !== $errorCode) {
        return false;
    }
    return $output;
}

function success($messages=null,$closeCurrent=false,$tabid="index-form"){
    $message = array();
    if(!$messages) $messages='操作成功！';
    $message['statusCode'] = "200";
    $message['message'] = "$messages";
    $message['tabid'] = $tabid;
    $message['closeCurrent'] = $closeCurrent;
    $message['forward'] = "";
    $message['forwardConfirm'] = "";
    return json_encode($message);
}

function error($messages=null){
    $message = array();
    if(!$messages) $messages='操作失败！';
    $message['statusCode'] = "300";
    $message['message'] = "$messages";
    return json_encode($message);
}